﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// PaginationDrawingObject.xaml 的交互逻辑
    /// </summary>
    public partial class PaginationDrawingObject : UserControl
    {
        public PaginationDrawingObject(Vant.CellTypes.Pagination pagination, IDrawingHelper drawingHelper)
        {
            var viewModel = new PaginationDrawingObjectViewModel();

            viewModel.NextText = pagination.nextText ?? Properties.Resources.Pagination_NextPage;
            viewModel.PreText = pagination.prevText ?? Properties.Resources.Pagination_PrePage;
            viewModel.Color = drawingHelper.GetBrush("Accent 1");
            var borderColor = drawingHelper.GetBrush("#EBEDF0");
            for (int i = 0; i < pagination.showPageSize; i++)
            {
                viewModel.Buttons.Add(new PagerButtonViewModel()
                {
                    Text = (i + 1).ToString(),
                    Background = (i == 0 ? viewModel.Color : Brushes.Transparent),
                    Foreground = (i != 0 ? viewModel.Color : Brushes.White),
                    BorderBrush = (i == 0 ? viewModel.Color : borderColor),
                });
            }
            if (pagination.forceEllipsis && pagination.showPageSize > 0)
            {
                viewModel.Buttons.Add(new PagerButtonViewModel()
                {
                    Text = "...",
                    Foreground = viewModel.Color,
                    BorderBrush = borderColor
                });
            }
            var last = viewModel.Buttons.LastOrDefault();
            if (last != null)
            {
                last.BorderThickness = new Thickness(0, 1, 0, 1);
            }
            viewModel.ButtonVisiblity = pagination.mode == "multi" ? Visibility.Visible : Visibility.Collapsed;
            viewModel.PagerVisibility = pagination.mode != "multi" ? Visibility.Visible : Visibility.Collapsed;
            viewModel.MiddleColumnGridWidth = pagination.mode == "multi" ? GridLength.Auto : new GridLength(1, GridUnitType.Star);
            viewModel.PagingText = "1/10";
            this.DataContext = viewModel;
            InitializeComponent();
        }
    }

    public class PaginationDrawingObjectViewModel
    {
        public GridLength MiddleColumnGridWidth { get; set; }
        public string PreText { get; set; }

        public List<PagerButtonViewModel> Buttons { get; set; } = new List<PagerButtonViewModel>();

        public Visibility PagerVisibility { get; set; }
        public Visibility ButtonVisiblity { get; set; }

        public string PagingText { get; set; }
        public Brush Color { get; set; }
        public string NextText { get; set; }
    }

    public class PagerButtonViewModel
    {
        public Brush BorderBrush { get; set; }
        public Thickness BorderThickness { get; set; } = new Thickness(0, 1, 1, 1);
        public Brush Background { get; set; }
        public Brush Foreground { get; set; }
        public string Text { get; set; }
    }
}
